package com.Policing.Polling;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.Policing.BaseActivity;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.Okhttps.HttpOkHttp;
import com.Policing.R;
import com.Policing.Since_case.Since_case;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class taskRelease extends BaseActivity {
    private TextView admin_tv,title_text,admin;
    private String id="";
    private String url = "http://" + Ip_Set.IP + "/api/taskRelease";
    private String userId= "";
    private String items="",routeId="";
    private String adminid="";
    private ImageView fh;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参值
        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的1.0
        p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的0.8
        getWindow().setGravity(Gravity.CENTER); // 设置靠
    }

    @Override
    public int intiLayout() {
        return R.layout.taskrelease;
    }

    @Override
    public void initView() {
        SharedPreferences mm = getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
        id = mm.getString("id", "");
        admin_tv = (TextView) findViewById(R.id.admin_tv);
        title_text = (TextView) findViewById(R.id.title_text);
        admin  = (TextView) findViewById(R.id.admin);
        title_text.setText("发布任务");
        fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(v -> finish());
        items = getIntent().getStringExtra("items");
        routeId = getIntent().getStringExtra("routeId");
    }

    @Override
    public void initData() {

    }
    public void Task(View view){
        Intent intent = new Intent(taskRelease.this,userQuery.class);
        startActivityForResult(intent,1);
    }
    public void Admin(View view){
        Intent intent = new Intent(taskRelease.this,userQuery.class);
        startActivityForResult(intent,2);
    }

    public  void isOK(View view){
        if (userId.equals("")){
            toastShort("请选择巡检人后重试");
            return;
        }

        upload();
      /*  Intent intent = new Intent();
        intent.putExtra("id",id);
        taskRelease.this.setResult(2,intent);
        taskRelease.this.finish();//把item的内容返回给之前的activity*/
    }
    private ProgressDialog progressDialog;
    private void showDialog(final Context context) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog = new ProgressDialog(context);
                progressDialog.setTitle("正在发布中");
                progressDialog.setMessage("Publishing...");
                progressDialog.setCancelable(false);
                progressDialog.show();
                //关闭进度圈圈
                // progressDialog.cancel();
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==1){
            admin_tv.setText(data.getExtras().getString("admin_name"));
            userId =   data.getExtras().getString("admin_id");

        }
        if (requestCode==2&&resultCode==1){
            admin.setText(data.getExtras().getString("admin_name"));
            adminid =   data.getExtras().getString("admin_id");

        }
    }
    private void upload(){
        showDialog(taskRelease.this);
        Map<String,String> map = new HashMap<>();
        map.put("routeId",routeId);
        map.put("userId",userId);
        map.put("admin",adminid);
        map.put("id",id);
        map.put("source", Since_case.set().getSource());
        HttpOkHttp.getInstance().requestPost(url, map, new HttpOkHttp.OkHttpCallBack<String>() {

            @Override
            public void requestSuccess(final String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.getString("state").equals("1")){
                                toastShort("发布成功");
                                progressDialog.cancel();
                                Intent intent = new Intent();
                                Log.i("11111111111",items);
                                intent.putExtra("items",items);
                                taskRelease.this.setResult(2,intent);
                                taskRelease.this.finish();//把item的内容返回给之前的activity
                            }else if (jsonObject.getString("state").equals("0")){

                                toastShort("发布失败");
                                progressDialog.cancel();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            progressDialog.cancel();
                        }
                    }
                });
            }

            @Override
            public void requestFailure(String message) {
                progressDialog.cancel();
            }
        },String.class);
    }
}
